function [F, g, G] = biggs6(x,m)
% biggs6 - 计算Biggs EXP6函数在x点的函数值，与梯度值。
% Call：
% [F, g, G] = biggs6(x, m)
%    [F, g] = biggs6(x, m)
%       [F] = biggs6(x, m)
% 输入参数：
% x     ：给定的点，
% m     ：m>=6的整数
% 输出参数：
% F     ：函数值
% g     ：梯度值，不要求不计算
% G     ：Hessen矩阵，不要求不计算
    [stop] = check(x);
    if stop, error('x must be a real valued vector with length 6'), end
    x = x(:);
    
    r = zeros(m,1);
    for i =1:m,
        t = i/10;
        r(i) = x(3)*exp(-t*x(1))-x(4)*exp(-t*x(2))+x(6)*exp(-t*x(5));
        y=exp(-t)-5*exp(-10*t)+3*exp(-4*t);
        r(i) = r(i)-y;
    end
    F = sum(r.^2);
    % 计算梯度：    
    if nargout>1,
        pr = zeros(6,m);
        g  = zeros(6,1);
        for i = 1:m,
            t = i/10;
            pr(1,i)= -t*x(3)*exp(-t*x(1));
            pr(2,i)= t*x(4)*exp(-t*x(2));
            pr(3,i)= exp(-t*x(1));
            pr(4,i)= -exp(-t*x(2));
            pr(5,i)= -t*x(6)*exp(-t*x(5));
            pr(6,i)= exp(-t*x(5));
        end
        for i =1:m,
            g = g + r(i)*pr(:,i);
        end
        g = 2*g;
    end
    % 计算Hessen矩阵
    if nargout>2,
        G = zeros(6, 6);
        Hr = zeros(6,6,m);
        for i = 1:m,
            t = i/10;
            Hr(1,1,i) = t^2*x(3)*exp(-t*x(1));
            Hr(1,3,i) = -t*exp(-t*x(1));
            Hr(3,1,i) = -t*exp(-t*x(1));
            
            Hr(2,2,i) = -t^2*x(4)*exp(-t*x(2));
            Hr(2,3,i) = t*exp(-t*x(2));
            Hr(3,2,i) = t*exp(-t*x(2));
            
            Hr(5,5,i) = t^2*x(6)*exp(-t*x(5));
            Hr(5,6,i) = -t*exp(-t*x(5));
            Hr(6,5,i) = -t*exp(-t*x(5));
            G = G+r(i)*Hr(:,:,i);
            for l = 1:6,
                for k = 1:6,
                    G(l,k) = G(l,k) + pr(l,i) * pr(k,i);
                end
            end
        end
        G = 2*G;
    end
end
%============  Auxiliary functions  ========================

function [err] = check(x)
% CHECK - check x
%   
    err = 0; sx = size(x); n = max(sx);
    if  (min(sx) ~= 1) | n~=6 | ~isreal(x) | any(isnan(x(:))) | isinf(norm(x(:))) 
        err = -1; 
    end    
end